package EmailService;
import DTO.Email;
import Exception.KwickKioskServiceException;
import Utilities.EmailTemplates;
import java.security.Security;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * This class sends email to  store customers. Order confirmation email, password recovery email
 * and registration confirmation email is sent to customers. This class uses java mail utility to send email.
 * @author tejinder
 * @see EmailService
 */

public class EmailSender {

        private static final String SMTP_HOST_NAME = "smtp.gmail.com";
        private static final String SMTP_PORT = "465";
        private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        /**
         * this method takes the email data transfer object and extract attributes to set in email subject,
         * email body and add secured socket layer  provider to the email message.
         * @param email an email data transfer object
         * @throws Exception
         */
         public static void sendEmail( Email email) throws KwickKioskServiceException, MessagingException{

                Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                  new EmailSender().sendSSLMessage(email.getTo(), email.getEmailSubjectTxt(), email.getEmailMsgTxt(), email.getFrom());
                    System.out.println("Sucessfully Sent mail to All Users");
        }

         /**
          * this method sends email message, by associating it with port  name, host name .
          * @param recipients array of recipients
          * @param subject email subject
          * @param message email body
          * @param from email from
          * @throws MessagingException
          */
        public void sendSSLMessage(String recipients[], String subject,
        String message, String from) throws MessagingException {

            boolean debug = true;

            Properties props = new Properties();
            props.put("mail.smtp.host", SMTP_HOST_NAME);
            props.put("mail.smtp.auth", "true");
           // props.put("mail.debug", "true");
            props.put("mail.smtp.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");

            Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(EmailTemplates.adminEmail, EmailTemplates.adminPwd);
                 }
             });

            //session.setDebug(debug);

            Message msg = new MimeMessage(session);
            InternetAddress addressFrom = new InternetAddress(from);
            msg.setFrom(addressFrom);

            InternetAddress[] addressTo = new InternetAddress[recipients.length];
            for (int i = 0; i < recipients.length; i++) {
                addressTo[i] = new InternetAddress(recipients[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);

            // Setting the Subject and Content Type
            msg.setSubject(subject);
            msg.setContent(message, "text/plain");
            Transport.send(msg);
        }
   }

